package com.hellobike.haxing.modules.order.listener;

import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.modules.order.entity.Order;
import com.hellobike.haxing.modules.order.mapper.OrderMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 取消成功订单
 */
@RocketMQMessageListener(topic = "t_order", consumerGroup = "backendSupplierStatusChange")
@Component
@Data
@Slf4j
public class OrderStatusListener implements RocketMQListener<Long> {

    @Autowired
    private OrderCancelListener orderCancelListener;

    @Autowired
    private OrderSuccessListener orderSuccessListener;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void onMessage(Long id) {
        Order order = orderMapper.selectById(id);
        if (order == null) return;
        try {
            Thread.sleep(3000);
        } catch (Exception ex) {
            log.error("", ex);
        }
        if (order.getStatus() == 3) {
            log.info("start prepare supplier");
            orderSuccessListener.onMessage(id);
        } else if (order.getStatus() == 2) {
            log.info("start cancel supplier");
            orderCancelListener.onMessage(id);
        }
    }
}
